<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/9/5
 * Time: 14:34
 */

namespace Application\Common\Controller;


use J\Schema\ICollection;
use J\Exception;
use J\Intent;
use J\MVC\Controller;

trait ElementUITrait
{
    /**
     * 执行ElementUI跳转
     *
     * @param Intent $intent
     * @param string $url
     * @param string $layer
     * @param string $msg
     */
    protected function elementUIRedirect($intent, $url, $layer = null, $msg = null)
    {
        $redirect = ['url' => $url];
        if ($layer) $redirect['layer'] = $layer;
        $intent->ajax(true, $msg, ['redirect' => $redirect]);
    }

    /**
     * 页面标题
     * @param ICollection $hisCollection
     * @param $values
     * @return string
     * @throws Exception
     */
    public function defaultTitle($hisCollection, $values)
    {
        $label = $this->getCollection()->getLabel();
        return $values[$hisCollection->getKey()] . '的' . $label;
    }

    /**
     * 查询参数处理接口
     *
     * @param string $value 元素值
     * @param string $name 元素名
     *
     * @return bool 查询是否有效
     *
     */
    public function parseInputValue(&$value, /** @noinspection PhpUnusedParameterInspection */$name = null)
    {
        return !empty($value);
    }

    /**
     * @see Controller::getCollection()
     * @param string $collection
     * @return ICollection
     * @throws Exception
     */
    abstract function getCollection($collection = null);
}